package com.surelution.core.drivers.kh_v2;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.surelution.core.drivers.CheckRecord;
import com.surelution.core.drivers.CommunicationException;
import com.surelution.core.drivers.Wand;
//import com.surelution.core.drivers.kh.KhLibrary;

public class KhWand implements Wand {
	
	public static final String LINE_SEPERATOR = ";";
	public static final String WORD_SEPERATOR = "|";

	private KhLibrary kh;
	public KhWand(KhLibrary kh) {
		this.kh = kh;
	}
	
	private String wandId;
	
	public String getId() {
		//SB device provider, read mac should be long format, read record should be short format
		kh.SetDataFmt(KhLibrary.LONG_DATA_FMT);
		if(wandId == null) { // to avoid punching the device 
			byte[] buffer = new byte[4];
			kh.doGetMacNo(4, buffer);
			String s = new String(buffer).trim();
			while(s.startsWith("0")) {
				s = s.substring(1);
			}
			wandId = s;
		}
		return wandId;
	}

	/* (non-Javadoc)
	 * @see com.surelution.core.drivers.Wand#setId(java.lang.String)
	 */
	public void setId(String id) {
		kh.SetDataFmt(KhLibrary.LONG_DATA_FMT);
		if(id != null && id.length() <= 4) {
			try {
				int v = Integer.parseInt(id, 16);
				if(v > 0 && v < 65535) {
					byte[] bs = id.getBytes();
					kh.doSetMacNo(id.length(), bs);
				}
			} catch(NumberFormatException e) {
				
			}
		}
	}

	public List<CheckRecord> getNewRecord() {
		kh.SetDataFmt(KhLibrary.SHORT_DATA_FMT);
		List<CheckRecord> cr = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int[] is = new int[1];
		kh.doCheckData(is);
		
		int count = is[0];
		byte[] buffer = new byte[count * 25];
		kh.doGetPatrolData(buffer.length, buffer);
		String ret = new String(buffer);
		System.out.println(ret);
		String[] lines = ret.split(LINE_SEPERATOR);
		if(lines != null) {
			cr = new ArrayList<CheckRecord>();
			for(String line : lines) {
				if(line != null && !"".equals(line.trim())) {
					String cardNo = line.substring(line.indexOf(WORD_SEPERATOR) + 1);
					try {
						Date date = sdf.parse(line.substring(0, line.indexOf(WORD_SEPERATOR)));
						CheckRecord record = new CheckRecord(0, 0, 0, date, cardNo);
						cr.add(record);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}
		}
		System.out.println(ret);
		return cr;
	}

	public void setTime(Date date) throws CommunicationException {
		//kai he can't set a specific time, but to sync the pc's time to device, shit
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		kh.doConfTime(time.length(), time.getBytes());
		kh.doSyncTime();
	}

	public Date getTime() throws CommunicationException {
		byte[] content = new byte[1024];
		kh.doGetTime(content.length, content);
		String s = new String(content);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.surelution.core.drivers.Wand#getDroppings()
	 */
	public List<Date> getDroppings() throws CommunicationException {
		List<Date> list = new ArrayList<Date>();
		int count = 300;
		byte[] buffer = new byte[count * 25];
		kh.doGetDropData(buffer.length, buffer);
		
		String ret = new String(buffer);
		ret = ret.trim();
		String[] lines = ret.split(";");
		if(lines != null) {
			for (String line : lines) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					list.add(sdf.parse(line));
				} catch (ParseException e) {
				}
			}
		}
		if(list.size() > 0) {
			String id = getId();
			kh.doClearDropData();
			setId(id);
		}
		return list;
	}

	public boolean isValid() {
		//just to verify the port is a valid Kh tour wand's port
		try {
			Date date = getTime();
			return date != null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.surelution.core.drivers.Wand#loadDefaultConfigure()
	 */
	public void loadDefaultConfigure() throws CommunicationException {
		kh.doSetVolType(KhLibrary.VT_Beep);
	}

	public void clearData() throws CommunicationException {
		kh.doClearPatrolData();
	}

}
